//
//  TypeView.m
//  TourPlay
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "TypeView.h"

@interface TypeView () <UIScrollViewDelegate>



@end

@implementation TypeView
- (void)dealloc
{
    [_myScrollV release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    self.myScrollV = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.myScrollV.showsHorizontalScrollIndicator = NO;
    
    // 取消边界反弹效果
    self.myScrollV.bounces = NO;
    self.myScrollV.delegate = self;
    [self addSubview:self.myScrollV];
    [_myScrollV release];
}
//- (UIScrollView *)myScrollV
//{
//    if (nil == _myScrollV) {
//        self.myScrollV = [[UIScrollView alloc] initWithFrame:self.bounds];
//        self.myScrollV.showsHorizontalScrollIndicator = NO;
//
//        // 取消边界反弹效果
//        self.myScrollV.bounces = NO;
//        self.myScrollV.delegate = self;
//    }
//    return _myScrollV;
//}
- (void)setTitleArr:(NSMutableArray *)titleArr
{
    if (_titleArr != titleArr) {
        [_titleArr release];
        _titleArr = [titleArr retain];
    }


    
    CGFloat scrollWidth = 0;
    
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        
        
        TypeTitleModel *titleMod = [self.titleArr objectAtIndex:i];
        CGFloat btnWidth = (CGFloat)([AppTools widthForContent:titleMod.titleName] + 18);
        
//        NSLog(@"title = %@ : %f", titleMod.titleName, btnWidth);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(scrollWidth, 0, btnWidth, self.frame.size.height);
        [button setTitle:titleMod.titleName forState:UIControlStateNormal];
        button.titleLabel.font = NAVG_HOME_FONT;
        if (0 == i) {
            [button setTitleColor:NAVG_FONTCOLOR_ON forState:UIControlStateNormal];
        } else {
            [button setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        }
        
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myScrollV addSubview:button];
        
        scrollWidth += btnWidth;
    }
    self.myScrollV.contentSize = CGSizeMake(scrollWidth, 0);
}
- (void)buttonAction:(UIButton *)sender
{
//    NSLog(@"----%ld", sender.tag - 1000);
    [self.delegate changeCollectionViewWithNum:sender.tag - 1000];
    [self changeButtonColorWithTag:sender.tag];
}
- (void)changeButtonColorWithTag:(NSInteger)tag
{
    for (UIView *view in self.myScrollV.subviews) {
        UIButton *button = (UIButton *)view;
        
        if (button.tag != tag && button.tag > 999) {
            [button setTitleColor:NAVG_FONTCOLOR_OFF forState:UIControlStateNormal];
        } else if (button.tag == tag) {
            [button setTitleColor:NAVG_FONTCOLOR_ON forState:UIControlStateNormal];
        }
    }
}
- (void)changemyScrollView:(NSInteger)num btnTag:(NSInteger)tag
{
    UIButton *button = (UIButton *)[self.myScrollV.subviews objectAtIndex:tag - 1000];
    
    // 计算偏移量
    CGPoint point = CGPointMake(num * button.frame.size.width - (self.myScrollV.frame.size.width - button.frame.size.width) / 2.f, 0);
    
    // 判断
    if (point.x < 0) {
        point.x = 0;
    } else if (point.x + self.myScrollV.frame.size.width > self.myScrollV.contentSize.width) {
        point.x = self.myScrollV.contentSize.width - self.myScrollV.frame.size.width;
    }
    // 偏移
    [self.myScrollV setContentOffset:point animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
